home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1941 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.3 KB  |  44 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overloading conversion
  5. Date: 14 Jan 1996 10:18:25 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan14111826@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4d934v$10hs@ds2.acs.ucalgary.ca>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: hdang@acs3.acs.ucalgary.ca's message of 13 Jan 1996 20:02:39 GMT
  12.  
  13. In article <4d934v$10hs@ds2.acs.ucalgary.ca> hdang@acs3.acs.ucalgary.ca (Hanna Dang) writes:
  14.  
  15.    Is there a way to overload functions such that certain
  16.    functions are called depending on the context the object are
  17.    used. For example,
  18.  
  19.    class T {
  20.      public:
  21.        int& int_value() { modified = TRUE; return value;}
  22.        int int_value() {return value;}
  23.     private:
  24.        int value;
  25.        int modified;
  26.    }
  27.  
  28.  
  29.    ...
  30.  
  31.    T a;
  32.    int test = a + 6 // int int_value should be called;
  33.    a = 20 // int& int_value should be called.
  34.  
  35.    However, this does not work because in both case int& int_value
  36.    is called.  Is there another way to find out if value is changed
  37.    or it's just inspected.
  38.  
  39. Yes -- make the latter 'int_value' a const member-function, ie.
  40.  
  41.         int int_value() const {return value;}
  42.  
  43.     Enno
  44.